package com.amkai.mobile.client;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.amkai.mobile.client.common.SharedDataProvider;
import com.amkai.mobile.common.job.organization.MobileOrganizationJobResult;

/**
 * 
 * @author peter
 * 
 */
public class Progress extends Activity {

	// Message types sent from the WebService Connection State Handler
	public static final int MESSAGE_CONNECTION_OK = 0;
	public static final int MESSAGE_CONNECTION_FAILED = 1;

	Context app;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.progress);
		app = this.getApplicationContext();
		final Progress progressScreen = this;
		final Intent login = new Intent(progressScreen, Login.class);
		Thread loginThread = new Thread() {
			int messageKind;

			@Override
			public void run() {
				synchronized (this) {
					// Wait given period of time or exit on touch

					MobileOrganizationJobResult result;
					try {

						result = SharedDataProvider.getInstance().getMojr(progressScreen);
						messageKind = MESSAGE_CONNECTION_OK;

						Bundle loginBundle = new Bundle();

						loginBundle.putSerializable("bojob_result", result);
						login.putExtras(loginBundle);
						if (messageKind == MESSAGE_CONNECTION_OK) {
							if (SharedDataProvider.getInstance().getPreferedAutoLogin(Progress.this)) {
								Bundle b = new Bundle();
								Long beId = SharedDataProvider.getInstance().getPreferedBusinessEntityId(Progress.this);
								if (beId != null) {
									b.putLong("department", beId);
									b.putLong("passport", SharedDataProvider.getInstance().getPassportId(Progress.this));
									Intent appointments = new Intent(Progress.this, Appointments.class);
									appointments.putExtras(b);
									startActivity(appointments);
								} else {
									startActivity(login);
								}
							} else {
								startActivity(login);

							}
						}
						Message msg = handler.obtainMessage(messageKind);
						handler.sendMessage(msg);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						messageKind = MESSAGE_CONNECTION_FAILED;
						Message msg = handler.obtainMessage(messageKind);
						Bundle b = new Bundle();
						b.putSerializable("exception", e);
						msg.setData(b);
						handler.sendMessage(msg);
					}
					// wait(5000);
				}

				// finish();

				// Run next activity if connection already
				
				stop();
			}

		};

		loginThread.start();

	}

	final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case MESSAGE_CONNECTION_OK:
				//Toast.makeText(app, "Every thing all right", Toast.LENGTH_SHORT).show();
				finish();
				break;
			case MESSAGE_CONNECTION_FAILED:
				//Exception e = null;
				if (msg.getData() != null) {
				// Bundle b = msg.getData();
				//	e = (Exception) b.get("exception");
					Toast.makeText(app, "Connection refused! Please try AGAIN!", Toast.LENGTH_SHORT).show();
				}
				AeMobile.startMain(Progress.this, true);
				finish();
				break;

			default:
				break;
			}

			// AlertDialog.Builder builder = new AlertDialog.Builder(app);
			// builder.setMessage(e.toString()).setCancelable(false).setPositiveButton("Yes",
			// new DialogInterface.OnClickListener() {
			// public void onClick(DialogInterface dialog, int id) {
			// Activity a = Progress.this.getParent();
			// Progress.this.finish();
			// a.finish();
			// }
			// }).setNegativeButton("No", new DialogInterface.OnClickListener()
			// {
			// public void onClick(DialogInterface dialog, int id) {
			// dialog.cancel();
			// }
			// });
			// AlertDialog alert = builder.create();
			// alert.show();

		}

	};

}
